package com.winit.openapi.wh.allocation.vaildator;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.outbound.vo.OutboundOrderEnumContants.OrderType;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wh.utils.ValidateUtils;

public class AllocationOrderfieldValidator {

    /**
     * 调拨单列表查询校验.
     * 
     * @param json
     */
    public static void validQueryByMainOrderParam(JSONObject json) {

        if (json.containsKey("orderNo")) {// 订单号
            ValidateUtil.validMaxLength(json, "orderNo", 128);
        }
        if (json.containsKey("orderStatus")) {// winit跟踪号
            ValidateUtil.validMaxLength(json, "orderStatus", 128);
        }
        if (json.containsKey("winitProductCode")) {// 调拨产品编号
            ValidateUtil.validMaxLength(json, "winitProductCode", 128);
        }
        if (json.containsKey("orderStatus")) {// 订单状态
            ValidateUtil.validMaxLength(json, "orderStatus", 128);
        }
        if (json.containsKey("orderType")) { //订单类型 存储仓调拨 ：SE   库间调拨：IL
        	 ValidateUtil.validMaxLength(json, "orderType", 128);
		}
       
    }

    public static void validCreateAllocationOrder(JSONObject json) {
        String winitProductCode = json.getString("winitProductCode");
        String orderType = getOrderType(winitProductCode);
        validCreateBaseField(json, orderType);
        validCreateSecondStepBaseField(json);
        validCreateThirdStepBaseField(json);
    }

    /**
     * 通过产品编码获得订单类型
     * 
     * @param winitProductCode 产品编码
     * @return
     */
    public static String getOrderType(String winitProductCode) {
        String orderType = null;
        /*if (winitProductCode.startsWith(OrderType.SD.getType())) {
            orderType = OrderType.SD.getType();
        } else if (winitProductCode.startsWith(OrderType.SP.getType())) {
            orderType = OrderType.SP.getType();
        } else if (winitProductCode.startsWith(OrderType.DS.getType())) {
            orderType = OrderType.DS.getType();
        }*/
        return orderType;
    }

    /**
     * 构建共用参数
     * 
     * @param json
     */
    public static void validCreateBaseField(JSONObject json, String orderType) {

        ValidateUtil.validMaxLength(json, "dispatchWhCode", 40, true);
        ValidateUtil.validMaxLength(json, "destinationWhCode", 40, true);
        ValidateUtil.validMaxLength(json, "orderNo", 64, false);
        ValidateUtil.validMaxLength(json, "customerCode", 64, true);
        /*// 标准出库验证
        if (OrderType.SD.getType().equals(orderType)) {

        } else if (OrderType.SP.getType().equals(orderType)) {

        } else if (OrderType.DS.getType().equals(orderType)) {

        } else {
            // 抛异常：{不支持的订单类型}
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, orderType);
        }*/
    }

    public static void validCreateSecondStepBaseField(JSONObject json) {
        JSONArray array = json.getJSONArray("merchandiseList");

        if (null == array || array.size() <= 0) {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "merchandiseList");
        }
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            ValidateUtil.validMaxLength(obj, "merchandiseCode", 64, true);
            ValidateUtil.validMaxLength(obj, "specification", 64, false);
            ValidateUtil.validMaxLength(obj, "quantity", 16, true);
            ValidateUtil.validInteger(obj, "quantity", true);// 需要校验必须为数字
        }
    }

    public static void validCreateThirdStepBaseField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "winitProductCode", 32, true);
    }
    /**
     * 校验创建海外仓出库单参数 第一步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveFirstStep
     * 
     * @param json
     */
    public static void validFirstStepField(JSONObject json) {
        /*ValidateUtil.validNotNull(json, "orderType");
        String orderType = json.getString("orderType");*/
        validBaseField(json, null);
    }
    public static void validAllocationOrder(JSONObject json) {
        //TODO 类型暂定
        validBaseField(json, OrderType.SD.getType());
        validSecondStepBaseField(json);
        validThirdStepBaseField(json);
    }
    /**
     * 构建共用参数
     * 
     * @param json
     */
    public static void validBaseField(JSONObject json, String orderType) {

        ValidateUtil.validMaxLength(json, "dispatchWhCode", 40, true);
        ValidateUtil.validMaxLength(json, "destinationWhCode", 40, false);
        ValidateUtil.validMaxLength(json, "orderNo", 64, false);
        // 标准出库验证
       
        /*if (OrderType.DS.getType().equals(orderType)) {

        } else {
            // 抛异常：{不支持的订单类型}
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, orderType);
        }*/
    }
    
    /**
     * 校验创建海外仓出库单参数 第二步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveSecondStep
     * 
     * @param json
     */
    public static void validSecondStepBaseField(JSONObject json) {
        JSONArray array = json.getJSONArray("merchandiseList");

        if (null == array || array.size() <= 0) {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "merchandiseList");
        }
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            ValidateUtil.validMaxLength(obj, "merchandiseCode", 64, true);
            ValidateUtil.validMaxLength(obj, "specification", 64, false);
            ValidateUtil.validMaxLength(obj, "qty", 16, true);
            ValidateUtil.isNotNull(obj, "qty");
            ValidateUtils.isNumber(obj.getString("expectQty"),"expectQty");
            ValidateUtils.isNumber(obj.getString("qty"), "qty"); // 校验必须是数字
        }
    }
    
    public static void validThirdStepBaseField(JSONObject json) {
        ValidateUtil.validMaxLength(json, "winitProductCode", 32, true);
    }
    
    public static void validSecondStepField(JSONObject json) {
        ValidateUtil.isNotNull(json, "orderNo");
        ValidateUtil.validMaxLength(json, "orderNo", 64, true);
        validSecondStepBaseField(json);
    }
    public static void validThirdStepField(JSONObject json){
        ValidateUtil.isNotNull(json, "orderNo");
        ValidateUtil.validMaxLength(json, "orderNo", 64, true);
    }

    public static void validCreateOutboundOrder(JSONObject json) {
        String winitProductCode = json.getString("winitProductCode");
        String orderType = getOrderType(winitProductCode);
        validCreateBaseField(json, orderType);
        validCreateSecondStepBaseField(json);
        validCreateThirdStepBaseField(json);
    }

	public static void validRecommendAllocation(JSONObject jsonObj) {
		ValidateUtil.validNotNull(jsonObj, "warehouseCode");
		validAllocationBatchOrder(jsonObj);
	}

	public static void validAllocationBatchOrder(JSONObject json) { 
		JSONArray array = json.getJSONArray("merchandiseList");

        if (null == array || array.size() <= 0)  {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "merchandiseList");    
        }
		for (Object item : array) {
			JSONObject obj = (JSONObject) item;
			validOrderMerchandise(obj);
		}
	}

	public static void validOrderMerchandise(JSONObject obj) {
            ValidateUtil.validMaxLength(obj, "merchandiseCode", 64, true);
            ValidateUtil.validMaxLength(obj, "specification", 64, false); 
            ValidateUtil.validMaxLength(obj, "qty", 16, true);
            ValidateUtil.validMaxLength(obj, "suggestQty", 16, true); 
            ValidateUtil.isNotNull(obj, "qty");
            ValidateUtil.isNotNull(obj,"type");
            ValidateUtil.isNotNull(obj,"isUrgent");
            ValidateUtils.isNumber(obj.getString("qty"), "qty"); // 校验必须是数字
            ValidateUtils.isNumber(obj.getString("suggestQty"), "suggestQty"); // 校验必须是数字
            ValidateUtils.isNumber(obj.getString("boxQty"), "boxQty");
	}

	public static void validRecommendmerchandise(JSONObject json) {
		ValidateUtil.validNotNull(json, "merchandiseCode");
	}
	
	/**
	 * 
	 * 第一步，校验越库信息和商品信息.
	 * @param json
	 */
    public static void validCrossDockFirstStepField(JSONObject json) {
        // 入库单号
        ValidateUtil.validNotNull(json, "inboundOrderNo");
        
        // 校验商品信息
        validSecondStepBaseField(json);
    }
}
